﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace UDPServerChooseReceive
{
    class Program
    {
        /// <summary>
        /// 用于UDP发送的网络服务类
        /// </summary>
        static UdpClient udpcRecv = null;

        static IPEndPoint localIpep = null;

        static Thread thrRecv;

        static void Main(string[] args)
        {
            localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8899); // 本机IP和监听端口号
            udpcRecv = new UdpClient(localIpep);
            thrRecv = new Thread(ReceiveMessage);
            thrRecv.Start();
            Console.WriteLine("UDP监听器已成功启动");

        }

        private static void ReceiveMessage()
        {
            byte[] bytRecv = udpcRecv.Receive(ref localIpep);
            string message = Encoding.Default.GetString(bytRecv, 0, bytRecv.Length);
            Console.WriteLine(string.Format("{0}[{1}]", localIpep, message));
        }


    }
}
